import http from "../../service/http"
const common = {
    user: function (that) {
        var user = wx.getStorageSync('user');
        //console.log(user)
        if (!user) {
            wx.getSetting({
                success: function(res){
                    if (res.authSetting['scope.userInfo']) {
                        // 已经授权，可以直接调用 getUserInfo 获取头像昵称
                        wx.login({
                            success: function (res) {
                                console.log(res)
                                if (res.code) {
                                    var udata = {
                                        code: res.code
                                    }
                                    http.req('/Wxapi/getOpenId', udata, function (res) {
                                        console.log(res)
                                        if (res) {
                                            wx.getUserInfo({
                                                success: function (userres) {
                                                    console.log(userres.userInfo)
                                                    wx.setStorageSync('userinfo', userres.userInfo);
                                                    var edata = {
                                                        openid: res.openid,
                                                        wx_name: userres.userInfo.nickName,
                                                        wx_img: userres.userInfo.avatarUrl
                                                    }
                                                    console.log(edata)
                                                    //return
                                                    http.req('/Wxapi/add_user', edata, function (res) {
                                                        console.log(res)
                                                        edata.userid = res.id
                                                        wx.setStorageSync('user', edata);
                                                        that.setData({
                                                            uinfo:edata
                                                        })
                                                        http.req('/Wxapi/zhanghao', {userid:res.id}, function (zhanghaors) {
                                                            if(zhanghaors.code =='2'){
                                                                that.setData({
                                                                    nodenglu:true
                                                                })
                                                            }else{
                                                                wx.setStorageSync('zhanghaoid', zhanghaors.zhanghaoid);
                                                                http.req("/Wxapi/chepai_list",{zhanghaoid:zhanghaors.zhanghaoid},function (res) {
                                                                    that.setData({
                                                                        list:res
                                                                    })
                                                                })
                                                            }

                                                        })
                                                        return edata

                                                    })
                                                }
                                            })
                                        }

                                    });
                                }
                            },

                        });
                    }else{
                        that.setData({
                            shouquan:1
                        })
                    }
                }
            })

        }else{
            http.req('/Wxapi/zhanghao', {userid:user.userid}, function (res) {
                console.log(res)
                if(res.code =='2'){
                    that.setData({
                        nodenglu:true
                    })
                }else{
                    wx.setStorageSync('zhanghaoid', res.zhanghaoid);
                }

            })
        }


    }


}
export default common;